package com.seaboxdata.interfaceJob.swkj;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.seaboxdata.entity.HistorySlData;
import com.seaboxdata.kafka.BaseStringKafkaProducer;
import com.seaboxdata.service.HistorySlDataService;
import com.seaboxdata.utils.TransRealHttpsHandler;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * 水务-渗流测站历史数据-入库17数据库dpswj_skhdjk，history_sl_data
 *
 * @Author wei
 * @Date 2021/8/18 15:40
 **/
@Component
@Configuration
@Slf4j
public class HistorySLDataJob {


    @Value("${swkj.url.historySlData}")
    private String url;

    @Value("${swkj.token.historySlData}")
    private String token;

    @Value("${swkj.topic.historySlData}")
    private String topic;

    @Value("${kafka.server}")
    private String kafkaServer;

    @Value("${interface.isTest}")
    private String isTest = "false";

    @Autowired
    private BaseStringKafkaProducer baseStringKafkaProducer;

    @Autowired
    private HistorySlDataService historySlDataService;

    private static final int retry = 3;

    private static int countRetry = 0;

    @Scheduled(cron = "${swkj.jobtime.historySlData}")//早晚9点各执行一次
    public void doCollect() {
        log.info("collect historySlData begin");
        //如果是测试,直接退出
        if ("true".equalsIgnoreCase(isTest)) {
            return;
        }
        try {
            List<HistorySlData> historySlDataList = retryGetData(url);
            if (!CollectionUtils.isEmpty(historySlDataList)) {
                //写入数据库
                Date date = new Date();
                for (HistorySlData historySlData : historySlDataList) {
                    historySlData.setCjsj(date);
                }
                historySlDataService.saveBatch(historySlDataList);
            }
        } catch (Exception e) {
            log.error("write historySlData error");
            e.printStackTrace();
        }
    }

    /**
     * 获取LIST
     *
     * @param url
     * @return
     * @throws Exception
     */
    public List<HistorySlData> retryGetData(String url) throws Exception {
        List<HistorySlData> historySlDataList = null;
        String result = "";
        try {
            result = TransRealHttpsHandler.sendPost(url, null);
            JSONObject jsonObject = JSONObject.parseObject(result);
            log.info("调用水务接口返回信息:"+jsonObject.toJSONString());
            log.info(jsonObject.getJSONObject("key").toJSONString());
            historySlDataList = TransRealHttpsHandler
                    .parseJSONObject(jsonObject.getJSONArray("data").toJSONString(), HistorySlData.class);
            if (CollectionUtils.isNotEmpty(historySlDataList)) {
                countRetry = 0;
            } else {
                if (countRetry == retry) {
                    log.error("we have try {} to getData,but still have error", countRetry);
                    return null;
                } else {
                    countRetry++;
                    Thread.sleep(1000);
                    historySlDataList = retryGetData(url);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return historySlDataList;
    }

}
